import { message } from 'antd';
import {
  getWagePayList,
  getPayProbList,
  salarySendTabNum,
  nsMarkIssued,
} from '@/services/salarymanage';

export default {
  state: {
    formFieldsValue: {},
    expandForm: false,
    activeKey: '1',
    tabList: [
      { itemCode: '1', itemName: '待收费', isHasBadge: false },
      { itemCode: '2', itemName: '待发放', isHasBadge: false },
      { itemCode: '3', itemName: '已发放', isHasBadge: false },
      { itemCode: '4', itemName: '待报税', isHasBadge: false },
      { itemCode: '5', itemName: '已报税', isHasBadge: false },
      { itemCode: '6', itemName: '问题人员', isHasBadge: false },
    ],
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 10,
      },
    },
    valuesArr: [
      'ordSndrId',
      'wageType',
      'incmType',
      'wageCityId',
      'taxCityId',
      'custId',
      'empName',
      'idCode',
      'creStartDate',
      'creEndDate',
      'creName',
    ]
  },
  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getWagePayList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取问题人员列表
    *fetchProList({ payload }, { call, put }) {
      const response = yield call(getPayProbList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'getList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取页签数量
    *fetchTabNum({ payload }, { call, put, select }) {
      const response = yield call(salarySendTabNum, payload.formFieldsValue);
      let activeKey = yield select(state => state.protection.activeKey);
      const tabList = yield select(state => state.protection.tabList);
      if (response.success) {
        if (response.data && response.data.length) {
          const sortNumber = (a, b) => a.itemCode - b.itemCode;
          const tabCodeArr = response.data.sort(sortNumber);
          const isHasBadgeCurrentTab = tabCodeArr.some(item => `${item.status}` === activeKey); // 当前页签有没有查询到
          if (!isHasBadgeCurrentTab) {
            activeKey = `${tabCodeArr[0].status}`;
          }
          tabCodeArr.forEach(item => {
            if (`${item.status}` !== activeKey) {
              tabList[item.status - 1].isHasBadge = true;
            }
          });
          yield put({
            type: 'updateModelData',
            payload: { activeKey, tabList },
          });
          const getListParams = {
            ...payload.formFieldsValue,
            pageArg: { currentPage: payload.currentPage, pageSize: payload.pageSize },
          };
          if (activeKey === '6') {
            // 问题人员
            yield put({
              type: 'fetchProList',
              payload: { ...getListParams, tab: activeKey },
            });
          } else {
            yield put({
              type: 'fetchList',
              payload: { ...getListParams, tab: activeKey },
            });
          }
        } else {
          yield put({
            type: 'getList',
            payload: {
              data: {},
              currentPage: 1,
              pageSize: 10,
            },
          });
        }
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },

    // 标记发放成功
    *markIssued({ payload }, { call }) {
      const response = yield call(nsMarkIssued, payload);
      return response;
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 设置查询时保存的值
    updateTabListQuery(state, { payload }) {
      const { tabList } = state;
      tabList.forEach((item, index) => {
        if (item.itemCode === payload.activeKey) {
          tabList[index].isHasBadge = false;
        }
      });
      return {
        ...state,
        tabList,
        ...payload,
      };
    },
  },
};
